package com.tangyee.person;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.tangyee.home.activity.Login;
import com.tangyee.home.activity.db.UsersInfo;
import com.tangyee.R;
import com.tangyee.utils.LocalCacheManager;

import org.litepal.LitePal;

import java.util.List;

public class setActivity extends AppCompatActivity implements View.OnClickListener {
    private List<UsersInfo> userdinfo;

    private TextView cacheText;//2019-1-27增加，alter by leeguiyang@yeah.net

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_person_set);
        findViewById(R.id.backToperson).setOnClickListener(this);
        findViewById(R.id.exit).setOnClickListener(this);
        findViewById(R.id.about_us).setOnClickListener(this);
        findViewById(R.id.version).setOnClickListener(this);
        findViewById(R.id.advise).setOnClickListener(this);
        findViewById(R.id.news).setOnClickListener(this);
        findViewById(R.id._clear).setOnClickListener(this);

        cacheText = (TextView) findViewById(R.id.appCacheText);
        cacheText.setText(LocalCacheManager.getTotalCacheSize(this));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.backToperson:
                finish();
                break;
            case R.id.exit:
                userdinfo = LitePal.findAll(UsersInfo.class);
                if (userdinfo.size() > 0) {
                    for (int i = 0; i < userdinfo.size(); i++) {
                        if (userdinfo.get(i).getStatus() == 1) {
                            userdinfo.get(i).setStatus(0);
                            userdinfo.get(i).save();
                        }
                    }
                    Toast.makeText(setActivity.this, "退出成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(this, Login.class);
                    //新建一个栈来存放被启动的activity.
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                }
                break;
            case R.id.about_us:
                Toast.makeText(setActivity.this, "你点击了关于我们", Toast.LENGTH_SHORT).show();
                break;
            case R.id.version:
                Toast.makeText(setActivity.this, "你点击了版本更新", Toast.LENGTH_SHORT).show();
                break;
            case R.id.advise:
                Toast.makeText(setActivity.this, "你点击了意见反馈", Toast.LENGTH_SHORT).show();
                break;
            case R.id.news:
                Toast.makeText(setActivity.this, "你点击了消息", Toast.LENGTH_SHORT).show();
                break;
            case R.id._clear:
//                Toast.makeText(setActivity.this, "你点击了清理内存", Toast.LENGTH_SHORT).show();
                LocalCacheManager.clearAllCache(this);
                cacheText.setText(LocalCacheManager.getTotalCacheSize(this));
                break;
        }
    }
}
